package com.team.cloudsharing.dao.backupfile;

import java.io.Serializable;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class BackUpFileDAOImpl implements BackUpFileDAO{
	
	final static Logger logger = Logger.getLogger(BackUpFileDAOImpl.class);

	@Autowired
	SessionFactory sessionFactory;

	@Override
	public long insertBackUpFile(BackUpFileDO backUpFileDO) {
		long backUpFileId = -1;
		Session session = null;
		if (backUpFileDO != null) {
			try {
				session = sessionFactory.openSession();
				Transaction tx = session.beginTransaction();
				session.saveOrUpdate(backUpFileDO);
				tx.commit();
				Serializable id = session.getIdentifier(backUpFileDO);
				backUpFileId = (Long) id;
			} catch (Exception e) {
				logger.error(e.getMessage());
			} finally {
				try {
					session.close();
				} catch (Exception e2) {
					logger.error(e2.getMessage());
				}
			}
		}
		return backUpFileId;
	}

	@Override
	public BackUpFileDO findBackUpFileById(long backUpFileId) {
		BackUpFileDO backUpFileDO = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			session.beginTransaction();
			backUpFileDO = (BackUpFileDO) session.get(BackUpFileDO.class, backUpFileId);
		} catch (Exception e) {
			logger.error(e.getMessage());
		} finally {
			try {
				session.close();
			} catch (Exception e2) {
				logger.error(e2.getMessage());
			}
		}
		return backUpFileDO;
	}
	
}
